<?php

/**
 * 文件注释
 *
 * Author: Liam
 * Datetime: 11/27/17 15:55 PM
 */

namespace ApiBundle\Controller;

use BaseBundle\Controller\BaseController;
use Symfony\Component\HttpFoundation\Request;

class ArticleController extends BaseController
{
    //    得到文章分类
    public function articleClassifyAction()
    {
        $conn = $this->getDoctrine()->getConnection();
        $data['info'] = $conn->fetchAll("SELECT * FROM article_classify");

        return $this->jsonResponse('操作成功',0, $data);
    }

    //   得到文章列表
    public function articleListAction(Request $request)
    {
        $classify_id = $request -> get('classify_id');

        if (!$classify_id) {
            return $this->jsonResponse('缺少参数',1);
        } else {
            $conn = $this->getDoctrine()->getConnection();
            $data['info'] = $conn->fetchAll("SELECT id,title,introduction,thumb,created_at FROM article WHERE article_classify_id = '". $classify_id ."'");
            $data['count'] = $conn->fetchColumn("SELECT count(*) FROM article WHERE article_classify_id = '". $classify_id ."'");

            return $this->jsonResponse('操作成功',0, $data);
        }
    }

    //   根据id得到文章详情
    public function articleInfoAction(Request $request)
    {
        $article_id = $request -> get('article_id');

        if (!$article_id) {
            return $this->jsonResponse('缺少参数',1);
        } else {
            $conn = $this->getDoctrine()->getConnection();
            $data['info'] = $conn->fetchAssoc("SELECT * FROM article WHERE id = '". $article_id ."'");

            return $this->jsonResponse('操作成功',0, $data);
        }
    }

    //  删除文章
    public function deleteArticleAction(Request $request)
    {
        $article_id = $request -> get('article_id');

        if (!$article_id) {
            return $this->jsonResponse('缺少参数',1);
        } else {
            $conn = $this->getDoctrine()->getConnection();
            $conn->delete("article", array("id" => $article_id));

            return $this->jsonResponse('删除成功',0);
        }
    }

    //  （新增）编辑文章
    public function editArticleAction(Request $request)
    {
        $id = $request -> get('id');
        $article_classify_id = $request -> get('classify_id');
        $title = $request -> get('title');
        $introduction = $request -> get('introduction');
        $content = $request -> get('content');
        $thumb = $request -> get('thumb');
        $created_at = date("Y-m-d H:i:s");

        $data = array(
            "id" => $id,
            "title" => $title,
            "article_classify_id" => $article_classify_id,
            "introduction" => $introduction,
            "content" => $content,
            "thumb" => $thumb,
            "created_at" => $created_at
        );

        $conn = $this->getDoctrine()->getConnection();

        if (!$id) {
            // 新增
            $conn->insert("article", $data);
        } else {
            // 编辑
            $conn->update("article", $data, array('id' => $id));
        }

        return $this->jsonResponse('操作成功', 0);
    }
}